<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>

import { REQUEST, SUCCESS } from 'app/shared/reducers/action-type.util';
import thunk from 'redux-thunk';
import axios from 'axios';
import sinon from 'sinon';
import configureStore from 'redux-mock-store';
import promiseMiddleware from 'redux-promise-middleware';

import profile, { ACTION_TYPES, getProfile } from 'app/shared/reducers/application-profile';

describe('Profile reducer tests', () => {
  const initialState = {
    ribbonEnv: '',
    inProduction: true,
    isOpenAPIEnabled: false
  };
  describe('Common tests', () => {
    it('should return the initial state', () => {
      const toTest = profile(undefined, {});
      expect(toTest).toEqual(initialState);
    });

    it('should return the right payload in prod', () => {
      const payload = {
        data: {
          'display-ribbon-on-profiles' : 'awesome ribbon stuff',
          activeProfiles : [ 'prod' ]
        }
      };

      expect(profile(undefined, { type: SUCCESS(ACTION_TYPES.GET_PROFILE), payload })).toEqual({
        ribbonEnv: 'awesome ribbon stuff',
        inProduction: true,
        isOpenAPIEnabled: false
      });
    });

    it('should return the right payload in dev with OpenAPI enabled', () => {
      const payload = {
        data: {
          'display-ribbon-on-profiles' : 'awesome ribbon stuff',
          activeProfiles : [ 'api-docs', 'dev' ]
        }
      };

      expect(profile(undefined, { type: SUCCESS(ACTION_TYPES.GET_PROFILE), payload })).toEqual({
        ribbonEnv: 'awesome ribbon stuff',
        inProduction: false,
        isOpenAPIEnabled: true
      });
    });
  });

  describe('Actions', () => {
    let store;

    const resolvedObject = { value: 'whatever' };
    beforeEach(() => {
      const mockStore = configureStore([thunk, promiseMiddleware]);
      store = mockStore({});
      axios.get = sinon.stub().returns(Promise.resolve(resolvedObject));
    });

    it('dispatches GET_SESSION_PENDING and GET_SESSION_FULFILLED actions', async () => {
      const expectedActions = [
        {
          type: REQUEST(ACTION_TYPES.GET_PROFILE)
        },
        {
          type: SUCCESS(ACTION_TYPES.GET_PROFILE),
          payload: resolvedObject
        }
      ];
      await store.dispatch(getProfile()).then(() => expect(store.getActions()).toEqual(expectedActions));
    });
  });
});
